<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>C语言基础 - 数据类型</title>
    <link rel="stylesheet" href="common2.css">
    <style>
        /* 页面特有补充样式 */
        .type-card {
            background-color: #f0f7ff;
            border-radius: 8px;
            padding: 15px;
            margin: 15px 0;
            border-left: 4px solid #4a90e2;
        }
        
        .type-table {
            width: 100%;
            margin: 20px 0;
        }
        
        .warning-note {
            background-color: #fff3e0;
            border-left: 4px solid #ff9800;
            padding: 12px;
            margin: 15px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>C语言基础入门课程</h1>
        </header>
        
        <nav>
            <a href="index.html">首页</a>
            <a href="02-intro.html">C语言简介与环境搭建</a>
            <a href="03-structure.html" class="current">C程序结构</a>
            <a href="04-datatypes.html">基本数据类型</a>
            <a href="05-variables.html">变量与常量</a>
            <a href="06-operators.html">运算符</a>
            <a href="07-io.html" class="current">输入输出</a>
            <a href="08-conditionals.html">条件语句</a>
            <a href="09-loops.html">循环结构</a>
            <a href="10-functions.html" class="current">函数基础</a>
            <a href="11-scope.html">作用域与存储类</a>
            <a href="12-recursion.html">递归函数</a>
            <a href="13-arrays.html">一维数组</a>
            <a href="14-multidim.html" class="current">多维数组</a>
            <a href="15-strings.html">字符串处理</a>
            <a href="16-pointers.html">指针基础</a>
            <a href="17-pointers_advanced.html" class="current">指针进阶</a>
            <a href="18-structs.html">结构体</a>
            <a href="19-files.html">文件操作</a>
            <a href="20-project.html">综合项目</a>
        </nav>
        
        <main>
            <h2>C语言数据类型</h2>
            
            <section>
                <h3>基本数据类型分类</h3>
                
                <div class="type-card">
                    <h4>1. 整型数据类型</h4>
                    <table class="type-table">
                        <tr>
                            <th>类型</th>
                            <th>关键字</th>
                            <th>大小(字节)</th>
                            <th>取值范围</th>
                        </tr>
                        <tr>
                            <td>字符型</td>
                            <td>char</td>
                            <td>1</td>
                            <td>-128 到 127 或 0 到 255</td>
                        </tr>
                        <tr>
                            <td>短整型</td>
                            <td>short</td>
                            <td>2</td>
                            <td>-32,768 到 32,767</td>
                        </tr>
                        <tr>
                            <td>整型</td>
                            <td>int</td>
                            <td>4</td>
                            <td>-2,147,483,648 到 2,147,483,647</td>
                        </tr>
                        <tr>
                            <td>长整型</td>
                            <td>long</td>
                            <td>4 或 8</td>
                            <td>-2,147,483,648 到 2,147,483,647 (4字节)</td>
                        </tr>
                    </table>
                    
                    <pre><code>// 整型变量声明示例
char c = 'A';
short s = 100;
int i = 1000;
long l = 100000L;</code></pre>
                </div>
                
                <div class="type-card">
                    <h4>2. 浮点型数据类型</h4>
                    <table class="type-table">
                        <tr>
                            <th>类型</th>
                            <th>关键字</th>
                            <th>大小(字节)</th>
                            <th>精度</th>
                        </tr>
                        <tr>
                            <td>单精度浮点</td>
                            <td>float</td>
                            <td>4</td>
                            <td>6-7位小数</td>
                        </tr>
                        <tr>
                            <td>双精度浮点</td>
                            <td>double</td>
                            <td>8</td>
                            <td>15-16位小数</td>
                        </tr>
                    </table>
                    
                    <pre><code>// 浮点型变量声明示例
float f = 3.14f;
double d = 3.1415926535;</code></pre>
                </div>
                
                <div class="type-card">
                    <h4>3. 字符型数据类型</h4>
                    <p>char类型实际上是一个小整数，存储ASCII码值：</p>
                    <pre><code>char ch1 = 'A';      // 字符常量
char ch2 = 65;       // ASCII码值
char ch3 = '\x41';   // 十六进制表示
char ch4 = '\101';   // 八进制表示</code></pre>
                    
                    <div class="warning-note">
                        <p><strong>注意：</strong> C语言没有专门的字符串类型，字符串使用字符数组表示。</p>
                    </div>
                </div>
            </section>
            
            <section>
                <h3>sizeof运算符</h3>
                <p>sizeof运算符用于获取数据类型或变量占用的内存大小(字节数)：</p>
                
                <pre><code>#include &lt;stdio.h&gt;

int main() {
    printf("char大小: %lu字节\n", sizeof(char));
    printf("int大小: %lu字节\n", sizeof(int));
    printf("float大小: %lu字节\n", sizeof(float));
    printf("double大小: %lu字节\n", sizeof(double));
    
    int x;
    printf("变量x大小: %lu字节\n", sizeof x);
    
    return 0;
}</code></pre>
                
                <div class="warning-note">
                    <p><strong>重要：</strong> sizeof的结果类型是size_t，打印时使用%lu格式说明符。</p>
                </div>
            </section>
            
            <section>
                <h3>类型转换</h3>
                
                <h4>1. 隐式类型转换(自动转换)</h4>
                <p>在不同类型混合运算时自动发生：</p>
                <pre><code>int i = 10;
float f = 3.5;
double d = i + f;  // i先转换为float，然后结果转换为double</code></pre>
                
                <h4>2. 显式类型转换(强制转换)</h4>
                <p>使用强制转换运算符：(type)expression</p>
                <pre><code>double d = 3.14159;
int i = (int)d;  // i的值为3，小数部分被截断</code></pre>
                
                <div class="warning-note">
                    <p><strong>类型转换规则：</strong></p>
                    <ol>
                        <li>char/short → int → unsigned → long → double ← float</li>
                        <li>赋值运算时，右边类型转换为左边类型</li>
                        <li>强制转换可能造成数据丢失或精度损失</li>
                    </ol>
                </div>
                
                <h4>类型转换示例</h4>
                <pre><code>#include &lt;stdio.h&gt;

int main() {
    int a = 5, b = 2;
    float result;
    
    result = a / b;          // 整数除法，result=2.0
    result = (float)a / b;   // 浮点除法，result=2.5
    
    printf("result1: %.1f\n", result);
    printf("result2: %.1f\n", result);
    
    return 0;
}</code></pre>
            </section>
        </main>
        

</body>
</html>